<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="最近打算做个小项目，刚好是疫情期间，到处都是二维码，于是决定做个二维码扫描识别项目，目前打算用mtcnn作为二维码的检测矫正，用densenet作为二维码的识别，看看是否可行。先看看mtcnn参考：How Do You Train a Face Detection Model?(番-强)MTCNN_face_detection_alignmentMTCNN-Tensorflowmxnet_mtcn">
<meta property="og:type" content="article">
<meta property="og:title" content="MTCNN阅读">
<meta property="og:url" content="http://zengzheming.gitee.io/2020/03/20/MTCNN%E9%98%85%E8%AF%BB/index.html">
<meta property="og:site_name" content="曾哲明的博客">
<meta property="og:description" content="最近打算做个小项目，刚好是疫情期间，到处都是二维码，于是决定做个二维码扫描识别项目，目前打算用mtcnn作为二维码的检测矫正，用densenet作为二维码的识别，看看是否可行。先看看mtcnn参考：How Do You Train a Face Detection Model?(番-强)MTCNN_face_detection_alignmentMTCNN-Tensorflowmxnet_mtcn">
<meta property="og:image" content="http://zengzheming.gitee.io/blog/2020/03/20/MTCNN%E9%98%85%E8%AF%BB/det1.prototxt.svg">
<meta property="og:image" content="http://zengzheming.gitee.io/blog/2020/03/20/MTCNN%E9%98%85%E8%AF%BB/det2.prototxt.svg">
<meta property="og:image" content="http://zengzheming.gitee.io/blog/2020/03/20/MTCNN%E9%98%85%E8%AF%BB/det3.prototxt.svg">
<meta property="og:image" content="http://zengzheming.gitee.io/blog/2020/03/20/MTCNN%E9%98%85%E8%AF%BB/det4.prototxt.svg">
<meta property="article:published_time" content="2020-03-20T12:55:00.000Z">
<meta property="article:modified_time" content="2020-03-31T14:52:29.406Z">
<meta property="article:author" content="Zengzheming">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://zengzheming.gitee.io/blog/2020/03/20/MTCNN%E9%98%85%E8%AF%BB/det1.prototxt.svg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://zengzheming.gitee.io/2020/03/20/MTCNN阅读/"/>





  <title>MTCNN阅读 | 曾哲明的博客</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">曾哲明的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">努力成为高级打杂</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blog/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://zengzheming.gitee.io/blog/2020/03/20/MTCNN%E9%98%85%E8%AF%BB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Zengzheming">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="曾哲明的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">MTCNN阅读</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-20T20:55:00+08:00">
                2020-03-20
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E4%BA%BA%E8%84%B8%E7%9B%B8%E5%85%B3%E7%AE%97%E6%B3%95/" itemprop="url" rel="index">
                    <span itemprop="name">人脸相关算法</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>最近打算做个小项目，刚好是疫情期间，到处都是二维码，于是决定做个二维码扫描识别项目，目前打算用mtcnn作为二维码的检测矫正，用densenet作为二维码的识别，看看是否可行。<br>先看看mtcnn<br>参考：<br><a href="https://towardsdatascience.com/how-do-you-train-a-face-detection-model-a60330f15fd5" target="_blank" rel="noopener">How Do You Train a Face Detection Model?(番-强)</a><br><a href="https://github.com/kpzhang93/MTCNN_face_detection_alignment" target="_blank" rel="noopener">MTCNN_face_detection_alignment</a><br><a href="https://github.com/AITTSMD/MTCNN-Tensorflow" target="_blank" rel="noopener">MTCNN-Tensorflow</a><br><a href="https://github.com/YYuanAnyVision/mxnet_mtcnn_face_detection" target="_blank" rel="noopener">mxnet_mtcnn_face_detection</a></p>
<h1 id="MTCNN模型结构"><a href="#MTCNN模型结构" class="headerlink" title="MTCNN模型结构"></a>MTCNN模型结构</h1><p>在官方的的github中有两个版本的mtcnn，分别是mtcnnv1和mtcnnv2，mtcnnv1有3层，分别是pnet，rnet和onet，mtcnnv2比mtcnnv1多了一层lnet。他们的网络结构如下：<br>mtcnnv1的pnet、rnet和onet的结构分别如下:<br><img src="/blog/2020/03/20/MTCNN%E9%98%85%E8%AF%BB/det1.prototxt.svg" alt="avatar" title="pnet"><img src="/blog/2020/03/20/MTCNN%E9%98%85%E8%AF%BB/det2.prototxt.svg" alt="avatar" title="rnet"><img src="/blog/2020/03/20/MTCNN%E9%98%85%E8%AF%BB/det3.prototxt.svg" alt="avatar" title="onet"><br>可以看到pnet的输入图片resize成1×3×12×12的图片，输出是用softmax判断是否有人脸和输出人脸的4点坐标，rnett的输入图片是根据pnet截取的图片resize成1×3×24×24的图片，输出和pnet相似，用softmax判断是否有人脸和输出人脸的4点坐标，onet的输入图片是根据rnet截取的resize成1×3×48×48的图片，作为最后的输出模型除了有用softmax判断是否有人脸和输出人脸的4点坐标，它还另外输出5组人脸关键点坐标。<br>lnet的结构比较复杂，lnet的作用主要是优化lanmark的坐标位置（官方github中有写：’MTCNNv2’ is an extended version (using iamge patch around each landmark outputted from stage 3 to make a precise regression).），如图:<br><img src="/blog/2020/03/20/MTCNN%E9%98%85%E8%AF%BB/det4.prototxt.svg" alt="avatar" title="lnet"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">num_box &#x3D; total_boxes.shape[0]</span><br><span class="line">patchw &#x3D; np.maximum(total_boxes[:, 2]-total_boxes[:, 0]+1, total_boxes[:, 3]-total_boxes[:, 1]+1)</span><br><span class="line">patchw &#x3D; np.round(patchw*0.25)</span><br><span class="line"></span><br><span class="line"># make it even</span><br><span class="line">patchw[np.where(np.mod(patchw,2) &#x3D;&#x3D; 1)] +&#x3D; 1</span><br><span class="line"></span><br><span class="line">input_buf &#x3D; np.zeros((num_box, 15, 24, 24), dtype&#x3D;np.float32)</span><br><span class="line">for i in range(5):</span><br><span class="line">    x, y &#x3D; points[:, i], points[:, i+5]</span><br><span class="line">    x, y &#x3D; np.round(x-0.5*patchw), np.round(y-0.5*patchw)</span><br><span class="line">    [dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph] &#x3D; self.pad(np.vstack([x, y, x+patchw-1, y+patchw-1]).T,</span><br><span class="line">                                                            width,</span><br><span class="line">                                                            height)</span><br><span class="line">    for j in range(num_box):</span><br><span class="line">        tmpim &#x3D; np.zeros((tmpw[j], tmpw[j], 3), dtype&#x3D;np.float32)</span><br><span class="line">        tmpim[dy[j]:edy[j]+1, dx[j]:edx[j]+1, :] &#x3D; img[y[j]:ey[j]+1, x[j]:ex[j]+1, :]</span><br><span class="line">        input_buf[j, i*3:i*3+3, :, :] &#x3D; adjust_input(cv2.resize(tmpim, (24, 24)))</span><br></pre></td></tr></table></figure><br>这里lnet的输入图片是根据0net输出的5点坐标，截取1/8个patchw范围的5张图resize成24×24，最后输入就是1×15×24×24作为输入<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">output &#x3D; self.LNet.predict(input_buf)</span><br><span class="line"></span><br><span class="line">pointx &#x3D; np.zeros((num_box, 5))</span><br><span class="line">pointy &#x3D; np.zeros((num_box, 5))</span><br><span class="line"></span><br><span class="line">for k in range(5):</span><br><span class="line">    # do not make a large movement</span><br><span class="line">    tmp_index &#x3D; np.where(np.abs(output[k]-0.5) &gt; 0.35)</span><br><span class="line">    output[k][tmp_index[0]] &#x3D; 0.5</span><br><span class="line"></span><br><span class="line">    pointx[:, k] &#x3D; np.round(points[:, k] - 0.5*patchw) + output[k][:, 0]*patchw</span><br><span class="line">    pointy[:, k] &#x3D; np.round(points[:, k+5] - 0.5*patchw) + output[k][:, 1]*patchw</span><br></pre></td></tr></table></figure><br>可以看到输出的就是5个关键点的坐标，由于没有lnet的相关论文，这里可能存在<code>tmp_index = np.where(np.abs(output[k]-0.5) &gt; 0.35)</code>偏移大的问题，猜想是<code>output[k][tmp_index[0]] = 0.5</code>这个做限制</p>
<h1 id="MTCNN模型训练"><a href="#MTCNN模型训练" class="headerlink" title="MTCNN模型训练"></a>MTCNN模型训练</h1><p>作者使用的数据集包括：<a href="http://shuoyang1213.me/WIDERFACE" target="_blank" rel="noopener">WIDER-FACE dataset</a>和<a href="http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html" target="_blank" rel="noopener"> CelebA dataset</a>数据集，作者使用WIDER-FACE训练人脸判断有无和人脸边框，再使用CelebA训练人脸关键点。但是CelebA中有些错误的数据集，tensorflow版本的一位作者改为使用<a href="http://mmlab.ie.cuhk.edu.hk/archive/CNN_FacePoint.htm" target="_blank" rel="noopener">Deep Convolutional Network Cascade for Facial Point Detection</a>数据集。这里以tensorflow版本的脚本作为说明训练步骤：</p>
<h2 id="PNet训练："><a href="#PNet训练：" class="headerlink" title="PNet训练："></a>PNet训练：</h2><h2 id="RNet训练："><a href="#RNet训练：" class="headerlink" title="RNet训练："></a>RNet训练：</h2><h2 id="ONet训练："><a href="#ONet训练：" class="headerlink" title="ONet训练："></a>ONet训练：</h2><!-- 
$$L_{i}^{\text {det}}=-\left(y_{i}^{\text {det}} \log \left(p_{i}\right)+\left(1-y_{i}^{\text {det}}\right)\left(1-\log \left(p_{i}\right)\right)\right)$$ -->

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2020/03/20/tf1-13%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/" rel="next" title="tf1.13环境搭建">
                <i class="fa fa-chevron-left"></i> tf1.13环境搭建
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2020/03/31/%E5%A7%BF%E6%80%81%E4%BC%B0%E8%AE%A1/" rel="prev" title="姿态估计">
                姿态估计 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Zengzheming</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/%7C%7Carchive">
              
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">5</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#MTCNN模型结构"><span class="nav-number">1.</span> <span class="nav-text">MTCNN模型结构</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#MTCNN模型训练"><span class="nav-number">2.</span> <span class="nav-text">MTCNN模型训练</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#PNet训练："><span class="nav-number">2.1.</span> <span class="nav-text">PNet训练：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#RNet训练："><span class="nav-number">2.2.</span> <span class="nav-text">RNet训练：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ONet训练："><span class="nav-number">2.3.</span> <span class="nav-text">ONet训练：</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Zengzheming</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
